package cn.swu.lzr.collection;

import cn.swu.lzr.JDBC.DBTools;
import cn.swu.lzr.event.Event;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.Session;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

public class displayCollection extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        ArrayList<Integer> ItemList = (ArrayList<Integer>) session.getAttribute("Collection");
        List<Event> events = new ArrayList<>();
        DBTools dbTools = new DBTools();
        for (Integer item : ItemList) {
            try {
                Event event = dbTools.GetEventById(item);
                events.add(event);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(events);


            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");

            try (Writer writer =  response.getWriter()){
                writer.write(json);
            } catch (Exception e)
        {
            e.printStackTrace();
        }


    }


}
